﻿// See https://aka.ms/new-console-template for more information
using System.Net;
using System.Net.Sockets;
using System.Text;

Console.WriteLine("Hello, World!");

//socket创建
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


//绑定服务器Ip和端口
socket.Bind(
        new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080)
        );
//监听模式
socket.Listen(1000);

Console.WriteLine("开启SocketService");

while (true)
{
    //接受客户端连接
    Socket socketClient = socket.Accept();

    byte[] buffer = new byte[1024];
    int byteRec = socketClient.Receive(buffer);

    string data=Encoding.ASCII.GetString(buffer);
    Console.WriteLine("获取数据："+ data);

    //发送消息
    byte[] msg= Encoding.ASCII.GetBytes("get data");
    socketClient.Send(msg);

    //关闭连接
   // socketClient.Shutdown(SocketShutdown.Both);
    //socketClient.Close();

}
